package org.lovelandhs.robotics.y2010.input;

import java.util.Vector;

/**
 *
 * @author Robotics
 */
public class Axis {

	public static final double DEFAULT_THRESHOLD = 0.02;
	public static final double DEFAULT_RANGE_MIN = -1.0;
	public static final double DEFAULT_RANGE_MAX = 1.0;

	private String name;
	private int number;
	private double value;

	private double threshold;
	private double rangeMin;
	private double rangeMax;

	private Vector listeners;

	public Axis(int number, String name) {
		this.number = number;
		this.name = name;

		threshold = DEFAULT_THRESHOLD;
		rangeMin = DEFAULT_RANGE_MIN;
		rangeMax = DEFAULT_RANGE_MAX;

		listeners = new Vector();
	}

	public Axis(int number) {
		this(number, String.valueOf(number));
	}

	public String getName() {
		return name;
	}

	public int getNumber() {
		return number;
	}

	public double getValue() {
		return value;
	}

	public double getRangeMax() {
		return rangeMax;
	}

	public void setRangeMax(double rangeMax) {
		this.rangeMax = rangeMax;
	}

	public double getRangeMin() {
		return rangeMin;
	}

	public void setRangeMin(double rangeMin) {
		this.rangeMin = rangeMin;
	}

	public double getThreshold() {
		return threshold;
	}

	public void setThreshold(double threshold) {
		this.threshold = threshold;
	}

	public void addListener(AxisListener listener) {
		listeners.addElement(listener);
	}

	public void removeListener(AxisListener listener) {
		listeners.removeElement(listener);
	}

	public AxisListener getListener(int index) {
		return (AxisListener) listeners.elementAt(index);
	}

	public void notifyUpdated(double value) {
		double diff = Math.max(getValue(), value) - Math.min(getValue(), value);
		if (diff > threshold) {
			this.value = value;
		} else {
			return;
		}

		for (int i = 0; i < listeners.size(); i++) {
			getListener(i).onAxisUpdated(this, value);
		}
	}

}
